'''
Created on April 7, 2011

@author: yc30v1m
'''
from logging import getLogger
import os
from glob import glob
from ConfigParser import ConfigParser
from library.Utils import Utils
from library.Utils import singleton

log = getLogger(__name__)

@singleton
class Configuration(object):
    '''
    reads all configuration files in folder
    '''
    __configFolder__ = 'configuration'
    __fileExt__ = 'ini'

    def __init__(self):
        self.__parser = ConfigParser()
        self.setConfigFolder(Utils().baseAppPath())
        self.__readAllConfigurations()
        
    def __readAllConfigurations(self):
        iniList = glob(self.__absConfigFolder__ + '*.' + self.__fileExt__)
        log.debug('Retrived Configuration Files are: ' + str(iniList))
        self.__parser.read(iniList)
        
    def setConfigFolder(self,path):
        self.__absConfigFolder__ = path + self.__configFolder__ + os.sep

    # Depericating this function will remove in next version        
    def __getOptionFromSection(self,sectionName, optionName, isBooleanOption=False):
        log.debug("Searching for Section :" + sectionName + " Option :" + optionName)
        if self.__parser.has_option(sectionName, optionName):
            if isBooleanOption == False:
                return self.__parser.get(sectionName, optionName)
            if isBooleanOption == True:
                return self.__parser.getboolean(sectionName, optionName)
        log.warning('Section and Options does not exist : ' + sectionName + ' Option : ' + optionName )
        return False
    
    ''' 
    if the sections are inherited from other sections
    first look in the corresponding section and then look in the base section
    if both fails return False
    Else return as per the hierarchy of sections 
    '''
    def getOptionFromSection(self,sectionName,optionName,isBooleanOption=False):
        log.debug("Looking for inherited option in Section : " + sectionName)
        inheritedSectionName = self._getOptionFromSection(sectionName, 'inherit')
        directSectionValue = self._getOptionFromSection(sectionName, optionName, isBooleanOption)
        if directSectionValue != False:
            return directSectionValue
        if inheritedSectionName != False:
            inheritedOptionValue = self._getOptionFromSection(inheritedSectionName, optionName, isBooleanOption)
            return inheritedOptionValue
        return False
         
    ''' 
    supporting function to retrive Inhertied options
    '''   
    def _getOptionFromSection(self,sectionName, optionName, isBooleanOption=False):
        log.debug("Searching for Section :" + sectionName + " Option :" + optionName)
        if self.__parser.has_option(sectionName, optionName):
            if isBooleanOption == False:
                return self.__parser.get(sectionName, optionName)
            if isBooleanOption == True:
                return self.__parser.getboolean(sectionName, optionName)
        log.warning('Section and Options does not exist : ' + sectionName + ' Option : ' + optionName )
        return False
    
    def getSection(self,sectionName):
        log.debug("Looking for Section :" + sectionName)
        if self.__parser.has_section(sectionName):
            log.debug("Configuration Section found :" + sectionName)
            return self.__parser.items(sectionName)
        log.warning('Section Does not exist : ' + str(sectionName))
        return False
    
    def getParser(self):
        return self.__parser